# 基础镜像 - 用于运行时
FROM mcr.microsoft.com/dotnet/aspnet:8.0 AS base

# 创建工作目录并设置权限
RUN mkdir -p /app && \
    adduser -u 1000 --disabled-password --gecos "" appuser && \
    chown -R appuser /app
USER appuser
WORKDIR /app

# 暴露端口
EXPOSE 8080
EXPOSE 8081

# 构建镜像 - 用于编译应用
FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build
ARG BUILD_CONFIGURATION=Release
WORKDIR /src

# 复制解决方案文件和项目文件
COPY ["*.sln", "."]
COPY ["DevProtocolApi/*.csproj", "DevProtocolApi/"]
COPY ["../Infrastructure/*.csproj", "Infrastructure/"]
COPY ["../Service/*.csproj", "Service/"]

# 恢复依赖
RUN dotnet restore

# 复制所有源代码
COPY . .

# 构建解决方案
RUN dotnet build -c $BUILD_CONFIGURATION -o /app/build

# 发布阶段 - 生成发布文件
FROM build AS publish
ARG BUILD_CONFIGURATION=Release
RUN dotnet publish "DevProtocolApi/DevProtocolApi.csproj" -c $BUILD_CONFIGURATION -o /app/publish /p:UseAppHost=false

# 最终镜像 - 包含运行时和发布文件
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "DevProtocolApi.dll"]       